<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar fixed>
      <template #title>
        <van-button class="search-btn" round type="info" icon="search" size="small" to="/search">搜索</van-button>
      </template>
    </van-nav-bar>

    <!-- 频道标签 -->
    <!--  v-model: 默认绑定选中标签的索引值
          animated: 是否开启切换标签内容时的转场动画
          swipeable: 是否开启手势滑动切换
          swipe-threshold 设置最低滑动标签数, 一把情况下通过页面上样式的不同来进行灵活的配置 -->
    <van-tabs class="channel-tabs"  v-model="active" animated swipeable swipe-threshold='3'>
      <van-tab
        v-for="obj in channels"
        :key="obj.id"
        :title="obj.name"
      >
        <!-- 频道于列表之间的最有体现 - 每个频道对应一个列表, 互不干涉 -->
        <ArticleList :channel="obj"></ArticleList>
      </van-tab>

      <!-- 通过插槽设置汉堡栏 - 固定在右侧的按钮 -->
      <template #nav-right>
        <!-- 占位符 -->
        <div class="placeholder"></div>
        <div class="hamburger-btn" @click="isChannelEditShow=true">
          <i class="toutiao toutiao-gengduo"></i>
        </div>
      </template>
    </van-tabs>

    <!-- 频道编辑弹出层 -->
    <van-popup
      class="edit-channel-popup"
      v-model="isChannelEditShow"
      position="bottom"
      :style="{ height: '100%' }"
      closeable
      close-icon-position="top-left"
    >
      <ChannelEdit
        :active="active"
        :myChannels="channels"
        :allChannels="allChannels"
        @updateActive="updateActiveFn"
      ></ChannelEdit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannelsAPI, getAllChannelsAPI } from '../../api/index.js'
import ArticleList from './components/articleList.vue'
import ChannelEdit from './components/channelEdit.vue'
import { getItem } from '../../utils/storage.js'
export default {
  name: 'homeIndex',
  components: {
    ArticleList,
    ChannelEdit
  },
  data () {
    return {
      active: 0,
      channels: [], // 频道列表
      allChannels: [], // 全部拼搭列表
      isChannelEditShow: false // 弹出层状态
    }
  },
  created () {
    // 调用获取频道列表
    this.loadChannels()
    // 调用获取全部频道列表
    this.loadAllChannels()
  },
  methods: {
    // 遮罩层切换频道操作
    // 在形参中以赋值形式添加默认值,
    updateActiveFn (index, isShow = false) {
      this.active = index
      // 如果isShow为true, 则是编辑页面删除频道时触发的方法, 不需要关闭弹出层
      // 如果为false, 则是弹出层跳转频道, 需要关闭弹出层
      if (!isShow) {
        this.isChannelEditShow = false
      }
    },
    // 加载频道列表数据
    async loadChannels () {
      try {
        // 如果有user.token 或者没有本地缓存 => 调用接口, 反之其他情况则取本地缓存数据
        const token = this.$store.state.user.token
        const localChannels = getItem('TOUTIAO_CHANNELS')
        if (token || !localChannels) {
          const res = await getUserChannelsAPI()
          console.log(res.data.data.channels)
          this.channels = res.data.data.channels
        } else {
          // 未登录时,判断是否有本地频道列表数据
          // 有,就拿来使用; 没有就调用接口(接口同登录获取是一个,不强制用户登录，匿名用户返回后台设置的默认频道列表)
          this.channels = localChannels
        }
      } catch (error) {
        console.log(error)
      }
    },
    // 获取全部频道列表
    async loadAllChannels () {
      try {
        const res = await getAllChannelsAPI()
        console.log(res.data.data.channels)
        this.allChannels = res.data.data.channels
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  padding-top: 180px;
  padding-bottom: 100px;
  :deep(.van-nav-bar__title) {
    // 取消顶部导航的标题插槽宽度限制
    max-width: unset;
  }
  .search-btn{
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    width: 500px;
    .van-icon-search {
      font-size: 32px;
      color: #fff;
    }
  }

  :deep(.channel-tabs) {
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      left: 0;
      right: 0;
      z-index: 1;
    }
    .van-tab {
      min-width: 200px;
      font-size: 30px;
      border-right: 1px solid #edeff3;
    }
    .van-tabs__line {
      width: 31px;
      height: 6px;
      background-color: #3296fa;
    }
    .placeholder {
      // flex布局时, 当某个元素希望不受到影响时, 可以设置flex-shrink, 不参与flex的计算
      flex-shrink: 0;
      width: 88px;
      height: 66px;
    }
    .hamburger-btn {
      position: fixed;
      right: 0;
      height: 88px;
      width: 66px;
      background-color: #fff;
      display: flex;
      justify-content: center;
      align-items: center;

      // 通过伪元素选择器来设置汉堡标签最左侧的竖线
      &::before {
        content: '';
        position: absolute;
        left: 0;
        width: 2px;
        height: 58px;
        background: url('../../assets/gradient-gray-line.png');
        background-size: contain;
      }
    }
  }

  .edit-channel-popup {
    // 让关闭按钮和弹出层内容分开
    box-sizing: border-box;
    padding-top: 100px;
  }
}
</style>
